import { goto } from '../goto';
import { TaPost } from '../script/ajax';
import { TaCache, TaToast } from '../script/tools';

// 用户订单接口
export const TaDataOrder = {

	// 创建订单
	add(data: any = {rules:'',carts:'',store_id:''}): Promise<any> {
		data.agent = TaCache.get('auth.from') || 0
		return new Promise((resolve, reject) => {
			TaPost('/shop/api.auth.order/add', data, '提交中...').then((ret: any) => {
				resolve(ret.data), goto.payment(ret.data.order_no, true)
			}).catch(reject)
		})
	},

	// 获取订单内容
	get(data: any = {}): Promise<any> {
		return TaPost('/shop/api.auth.order/get', data)
	},

	// 获取订单统计
	total(data: any = {}): Promise<any> {
		return TaPost('/shop/api.auth.order/total', data)
	},

	// 获取支付通道
	channle(data: any = {}): Promise<any> {
		return TaPost('/shop/api.auth.order/channel', data)
	},

	// 确认收货地址
	prefect(data: any = {}): Promise<any> {
		return TaPost('/shop/api.auth.order/perfect', data);
	},

	// 获取支付参数
	payment(data: any = {}): Promise<any> {
		return TaPost('/shop/api.auth.order/payment', data, '发起支付...')
	},

	// 确认订单收货
	confirm(data: any = {}): Promise<any> {
		return TaPost('/shop/api.auth.order/confirm', data, '确认收货...')
	},

	// 取消未支付订单
	cancel(data: any = {}) {
		return TaPost('/shop/api.auth.order/cancel', data, '取消订单...')
	},

	// 移除已取消订单
	remove(data: any = {}) {
		return TaPost('/shop/api.auth.order/remove', data, '移除订单...')
	},

	// 添加购物车
	cartSet(data: object = { ghash: '', number: 1, store_id:'' }): Promise<any> {
		return TaPost('/shop/api.auth.cart/set', data)
	},
	
	// 添加购物车
	cartClear(data: object = {store_id:'' }): Promise<any> {
		return TaPost('/shop/api.auth.cart/clear', data)
	},

	// 获取购物车
	cartGet(): Promise<any> {
		return TaPost('/shop/api.auth.cart/get', {}, '加载中...')
	},

	// 发起微信支付
	jsapi(response: any = {}): Promise<any> {
		return new Promise((resolve, reject) => {
			// @ts-ignore
			if (typeof WeixinJSBridge === 'undefined') {
				if (document.addEventListener) {
					document.addEventListener('WeixinJSBridgeReady', onWechatBridgeReady, false)
					// @ts-ignore
				} else if (document.attachEvent) {
					// @ts-ignore
					document.attachEvent('WeixinJSBridgeReady', onWechatBridgeReady)
					// @ts-ignore
					document.attachEvent('onWeixinJSBridgeReady', onWechatBridgeReady)
				}
			} else {
				onWechatBridgeReady();
			}

			function onWechatBridgeReady() {
				if (!response.package) return reject();
				// @ts-ignore
				WeixinJSBridge.invoke('getBrandWCPayRequest', response, (res: any) => {
					console.log('GetBrandWCPayRequest: ', res);
					let errMsg = res.errMsg || res.err_msg || ''
					if (errMsg === 'get_brand_wcpay_request:ok') {
						// 使用以上方式判断前端返回,微信团队郑重提示：
						// res.err_msg 将在用户支付成功后返回 ok，但并不保证它绝对可靠。
						return TaToast.success('支付成功！').then(() => resolve(res))
					}
					// 用户取消
					if (errMsg === 'get_brand_wcpay_request:cancel') {
						return TaToast.error('取消支付！').then(() => reject(res))
					}
					// 支付失败
					if (errMsg === 'get_brand_wcpay_request:fail') {
						return TaToast.error('支付失败！').then(() => reject(res))
					}
					TaToast.error('无法发起支付！').then(() => reject(res))
				});
			}
		})
	},

	// 订单类型
	types() {
		return JSON.parse(JSON.stringify([{
			name: '全部',
			page: 0,
			nums: 0,
			stat: '',
			list: [],
			more: 'loadmore',
		}, {
			name: '待付款',
			page: 0,
			nums: 0,
			stat: '2,3',
			list: [],
			more: 'loadmore',
		}, {
			name: '待发货',
			page: 0,
			nums: 0,
			stat: '4',
			list: [],
			more: 'loadmore',
		}, {
			name: '待收货',
			page: 0,
			nums: 0,
			stat: '5',
			list: [],
			more: 'loadmore',
		}, {
			name: '已完成',
			page: 0,
			nums: 0,
			list: [],
			stat: '6,7',
			more: 'loadmore',
		}]))
	}
}